package com.android.tea;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.ZoomDensity;
import android.widget.ProgressBar;

import com.android.tea.util.MyWebViewClient;
import com.android.tea.util.StringUtils;
import com.android.tea.util.TeaLog;
import com.android.tea.widght.TeaWebView;

public class BaseWebViewActivity extends Activity {
	protected static final String TAG = "BaseWebViewActivity";
	protected WebView webView;
	private ProgressBar progressBar;
	protected String initUrl = null;
	protected String currentUrl = null;
	protected int layoutId = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TeaEncyclopediaApp.list.add(this);

		setView();

		initURL();

		initLayout();
		// TeaLog.e(TAG, "initUrl is " + initUrl);
		if (StringUtils.isNotNullOrEmpty(initUrl)
				&& URLUtil.isNetworkUrl(initUrl)) {
			webView.loadUrl(initUrl);
		}
	}

	protected void setView() {
		setContentView(layoutId);
	}

	protected void initURL() {
		// 初始化Url
	}

	private void initLayout() {
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		initWebView();
		initCache();
	}

	protected void initCache() {
		webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
	}

	protected void initWebView() {
		webView = (TeaWebView) findViewById(R.id.webView);
		webView.getSettings().setDefaultZoom(ZoomDensity.MEDIUM);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webView.getSettings().setBlockNetworkImage(true);
		webView.setWebChromeClient(webChromeClient);
		webView.setWebViewClient(webViewClient);
	}

	WebViewClient webViewClient = new MyWebViewClient() {

		@Override
		public void doUpdateVisitedHistory(WebView view, String url,
				boolean isReload) {
			TeaLog.e(TAG, "doUpdateVisitedHistory url is " + url);
			if (progressBar != null)
				progressBar.setVisibility(View.GONE);
			super.doUpdateVisitedHistory(view, url, isReload);
		}

		@Override
		public void onLoadResource(WebView view, String url) {
			super.onLoadResource(view, url);
		}

		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler,
				SslError error) {
			handler.proceed();
		}

		@Override
		public void onScaleChanged(WebView view, float oldScale, float newScale) {
			super.onScaleChanged(view, oldScale, newScale);
		}

		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
			TeaLog.e(TAG, "onPageStarted url is " + url);
		};

		public void onPageFinished(WebView view, String url) {
			webView.getSettings().setBlockNetworkImage(false);
			int indexOf = url.indexOf("?");
			// if(-1!=indexOf) {
			// currentUrl = url.substring(0, indexOf);
			// }else {
			currentUrl = url;
			// }
			TeaLog.e(TAG, "currentUrl is " + currentUrl + " indexof is "
					+ indexOf);
			// view.loadUrl(url);
		};

	};

	WebChromeClient webChromeClient = new WebChromeClient() {

		public void onProgressChanged(WebView view, int progress) {
			super.onProgressChanged(view, progress);
			if (view.getUrl() == null
					|| "about:blank".equalsIgnoreCase(view.getUrl())) {
				progressBar.setVisibility(View.VISIBLE);
				progressBar.setProgress(10);
				return;
			}
			progressBar.setMax(100);
			if (progress < 100) {
				progressBar.setVisibility(View.VISIBLE);
				if (progress < 10) {
					progressBar.setProgress(10);
				} else {
					progressBar.setProgress(progress);
				}
			} else {
				progressBar.setProgress(100);
				progressBar.setVisibility(View.GONE);
			}
		}

		@Override
		public void onReceivedTitle(WebView view, String title) {
			super.onReceivedTitle(view, title);
		}

	};

	protected void stopLoading() {
		TeaLog.e(TAG, "stopLoading is running");
		webView.stopLoading();
	}

	@Override
	public void onBackPressed() {
		if (!initUrl.equals(currentUrl)) {
			webView.goBack();
		} else {
			// 退出程序
			Activity parent = this.getParent();
			if (parent != null) {
				parent.onBackPressed();
			} else {
				super.onBackPressed();
			}
		}
	}

}
